/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.PhieuGiaCongDTO;

/**
 *
 * @author tuan
 */
public class PhieuGiaCongDAO {
    private static final String MaPhieuGC = "MaPGC000";
    private static final int lengthMa = 8;
    
    public PhieuGiaCongDAO() {
    }
    
    public String getNewMaPhieuGiaCong() throws SQLException {
        String MaPGC = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllPhieuGiaCongFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MAPHIEUGC").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaPGC = MaPGC.concat(MaPhieuGC + maxMa);
        return MaPGC;
    }
    
    public PhieuGiaCongDTO getPhieuGCByMaPhieu(String MaPhieu) throws SQLException {
        Object[] objs = { MaPhieu };
        PhieuGiaCongDTO phieuGc = new PhieuGiaCongDTO();
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetPhieuGCByMaPhieuFromDatabase(?)", objs);
        while (rs.next()) {
            phieuGc.setMaPhieuDV(rs.getString("MAICHVU"));
            phieuGc.setMaPhieuGC(rs.getString("MAPHIEUGC"));
            phieuGc.setMaTho(rs.getString("MATHO"));
            phieuGc.setNgayGiao(rs.getDate("NGAYGIAO"));
            phieuGc.setNgayNhan(rs.getDate("NGAYNHAN"));
            phieuGc.setNgayThanhToan(rs.getDate("NGAYTHANHTOAN"));
        }
        
        return phieuGc;
    }
    
    public int updatePhieuGC(PhieuGiaCongDTO PhieuGC) {
        Object[] objs = { PhieuGC.getMaPhieuGC() , PhieuGC.getMaTho(), PhieuGC.getMaPhieuDV(), PhieuGC.getNgayNhan(),
                        PhieuGC.getNgayGiao(), PhieuGC.getNgayThanhToan() };
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdatePHIEUGCFromDataBase(?, ?, ?, ?, ?, ?)", objs);
    }
    
    public int deletePhieuGCByMaPhieuGC(PhieuGiaCongDTO PhieuGC) {
        Object[] objs = { PhieuGC.getMaPhieuGC() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeletePhieuGCByMaPhieuGCToDatabase(?)", objs);
    }
    
    public int insertPhieuGC(PhieuGiaCongDTO PhieuGC) {
        Object[] objs = { PhieuGC.getMaPhieuGC() , PhieuGC.getMaTho(), PhieuGC.getMaPhieuDV(), PhieuGC.getNgayNhan(),
                        PhieuGC.getNgayGiao(), PhieuGC.getNgayThanhToan() };
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertPHIEUGCFromDataBase(?, ?, ?, ?, ?, ?)", objs);
    }
    
}
